<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['quality:qualityFile:upload']"
        >
          上传
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleCheck"
          v-hasPermi="['quality:qualityFile:query']"
        >
          查看
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['quality:qualityFile:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#reworkFeeReport', '返工费用通知单')"
        >
          导出
        </el-button>
      </el-col>

      <!-- 分页效果 -->
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      id="reworkFeeReport"
      highlight-current-row
      border
      stripe
      :height="tableH"
      v-loading="loading"
      :data="ReworkFeeReportList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column
        fixed="left"
        label="#"
        type="index"
        width="40"
        align="center"
      />
      <el-table-column
        fixed="left"
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="系统编号"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="docDjbh"
      />
      <el-table-column
        label="返工单号"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="docMjbh"
      />
      <el-table-column
        label="申请日期"
        :show-overflow-tooltip="true"
        align="center"
        width="180"
        prop="docSyjt"
      >
        <template v-slot="scope">
          <span>{{
            parseTime(scope.row.docSyjt, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="180"
        align="center"
        prop="docWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        align="center"
        width="180"
        prop="itmWlpm"
      />
      <el-table-column
        label="返工数量"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="docJtdw"
      />
      <el-table-column
        label="不良现象"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="docDesc"
      />
      <el-table-column
        label="操作"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleCheck(scope.row)"
            v-hasPermi="['quality:qualityFile:query']"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="400px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <el-col :span="24">
          <el-form-item label="发生日期">
            <el-date-picker
              v-model="dateRangeDocSyjt"
              size="small"
              style="width: 215px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="返工单号" prop="docMjbh">
            <el-input
              v-model="queryParams.docMjbh"
              placeholder="请输入返工单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="物料代码" prop="docWldm">
            <el-input
              v-model="queryParams.docWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="不良描述" prop="docDesc">
            <el-input
              v-model="queryParams.docDesc"
              placeholder="请输入不良描述"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 上传品管检测卡对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="680px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="返工单号" prop="docMjbh">
            <el-input style="width: 500px" v-model="form.docMjbh" />
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="物料代码" prop="docWldm">
            <el-input disabled style="width: 150px" v-model="form.docWldm" />
            <el-input disabled style="width: 350px" v-model="form.itmPmgg" />
          </el-form-item>
        </el-col>
        <el-col :span="1" style="margin-left: -1px">
          <el-button
            class="addButton"
            @click="clickAddButton"
            type="primary"
            icon="el-icon-plus"
            circle
          />
        </el-col>
        <el-col :span="11">
          <el-form-item label="发生日期">
            <el-date-picker
              disabled
              v-model="form.docSyjt"
              type="date"
              style="width: 200px"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="返工数量" prop="docJtdw">
            <el-input style="width: 200px" v-model="form.docJtdw" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="不良现象">
            <el-input
              type="textarea"
              style="width: 500px"
              v-model="form.docDesc"
            />
          </el-form-item>
        </el-col>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" style="margin-right: 10px">取 消</el-button>
        <el-upload
          class="upload"
          action="#"
          :show-file-list="false"
          :http-request="uploadReport"
          style="display: inline-block"
        >
          <el-button
            :disabled="this.form.docJtdw == null"
            type="primary"
            slot="trigger"
            >上 传</el-button
          >
        </el-upload>
      </div>
    </el-dialog>

    <!-- 从列表中选择物料代码等信息 -->
    <el-dialog
      title="选择物料代码"
      v-model="openDetail"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form
        :model="queryParamsChild"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="筛选条件">
            <el-input
              v-model="queryParamsChild.param"
              clearable
              style="width: 370px"
              size="small"
              @input="clickAddButton"
            />
          </el-form-item>
        </el-col>

        <el-table
          highlight-current-row
          border
          stripe
          @scrollBottom="load"
          @row-click="clickTable"
          v-loading="loading"
          height="300"
          :data="materialsDetail"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column
            label="客户编码"
            width="135"
            align="center"
            prop="itmCustwldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmPmgg"
          />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmWldm"
          />
        </el-table>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancelDetail">取消</el-button>
            <el-button type="primary" @click="detailConfirm">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { exportTable, parseTime } from "@/utils/ruoyi";
import {
  deleteQualityFile,
  listDetectionCard,
  listMaterialsDetail,
  uploadQualityFile,
} from "@/api/quality/detectionCard";

export default {
  name: "ReworkFeeReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 上传按钮禁用
      upload: false,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      openDetail: false,
      // 总条数
      total: 0,
      totalAdd: 0,
      tableH: document.body.clientHeight - 320,
      // 返工费用通知单表格数据
      ReworkFeeReportList: [],
      materialsDetail: [],
      // 多选框选中数据
      selectionData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
        docType: "N",
        docWldm: null,
        docMjbh: null,
        params: null,
      },
      queryParamsChild: {
        pageNum: 1,
        pageSize: 25,
        param: null,
      },
      // 点击数据存储
      temporaryStorage: {
        docMjbh: null,
        docWldm: null,
        itmPmgg: null,
      },
      // 开单日期时间范围
      dateRangeDocSyjt: [
        new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 30 * 3),
        new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 30),
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        docMjbh: [
          { required: true, message: "返工单号不能为空", trigger: "blur" },
        ],
        docWldm: [
          { required: true, message: "物料代码不能为空", trigger: "blur" },
        ],
        docJtdw: [
          { required: true, message: "返工数量不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询返工费用通知单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRangeDocSyjt && "" != this.dateRangeDocSyjt) {
        this.queryParams.params["beginDocSyjt"] = this.moment(
          this.dateRangeDocSyjt[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endDocSyjt"] = this.moment(
          this.dateRangeDocSyjt[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      listDetectionCard(this.queryParams).then((res) => {
        this.ReworkFeeReportList = res.rows;
        this.total = res.total;

        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        docDjbh: null,
        docName: null,
        docType: null,
        docMjbh: null,
        docWldm: null,
        docTzbh: null,
        docJtdw: null,
        docSyjt: new Date(),
        docPath: null,
        docDesc: null,
        itmWlpm: null,
      };
      this.resetForm("form");
    },
    //load
    load() {
      if (
        this.queryParamsChild.pageNum <
        Math.ceil(this.totalAdd / this.queryParamsChild.pageSize)
      ) {
        this.queryParamsChild.pageSize = 25;
        this.queryParamsChild.pageNum++;
        listMaterialsDetail(this.queryParamsChild).then((res) => {
          this.materialsDetail = this.materialsDetail.concat(res.rows);
          this.totalAdd = res.total;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.openDetail = false;
      this.reset();
    },
    cancelDetail() {
      this.openDetail = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.docDjbhs = selection.map((item) => item.docDjbh);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.selectionData = selection;
    },
    /** 上传按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "上传返工费用通知单";
    },
    /** 查询按钮操作 */
    handleSearch() {
      this.reset();
      this.title = "查询返工费用通知单";
      this.openSearch = true;
    },
    /** 查看按钮操作 **/
    handleCheck(row) {
      let file =
        process.env.VUE_APP_BASE_API +
        (row.docPath || this.selectionData[0].docPath);
      window.open(file);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const docDjbhs = row.docDjbh || this.docDjbhs;
      this.$confirm(
        '是否确认删除返工费用通知单编号为"' + docDjbhs + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return deleteQualityFile(docDjbhs);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 添加界面点击事件 **/
    clickAddButton() {
      listMaterialsDetail(this.queryParamsChild).then((res) => {
        this.materialsDetail = res.rows;
        this.totalAdd = res.total;
      });
      this.openDetail = true;
    },
    /** 点击表事件 **/
    clickTable(row) {
      this.temporaryStorage = {};
      this.temporaryStorage.docMjbh = row.itmCustwldm;
      this.temporaryStorage.docWldm = row.itmWldm;
      this.temporaryStorage.itmPmgg = row.itmPmgg;
    },
    /** 物料添加界面确认事件 **/
    detailConfirm() {
      this.form.docWldm = this.temporaryStorage.docWldm;
      this.form.itmPmgg = this.temporaryStorage.itmPmgg;
      this.openDetail = false;
    },
    /** 上传处理 **/
    uploadReport(fileList) {
      const reader = new FileReader();

      reader.readAsDataURL(fileList.file);
      reader.onload = () => {
        let formData = new FormData();

        formData.append("file", fileList.file);
        formData.append("docType", this.queryParams.docType);
        formData.append("docMjbh", this.form.docMjbh);
        formData.append("docWldm", this.form.docWldm);
        formData.append("docJtdw", this.form.docJtdw);
        formData.append(
          "docSyjt",
          parseTime(this.form.docSyjt, "{y}-{m}-{d} {h}:{i}:{s}")
        );
        formData.append("docDesc", this.form.docDesc);

        uploadQualityFile(formData).then((res) => {
          if (res.code == 200) {
            this.$message.success("上传成功！");
          } else {
            this.$message.error("上传失败！");
            console.log(res);
          }
          this.getList();
          this.open = false;
        });
      };
    },
    /** 导出按钮操作 */
    handleExport(a, b) {
      exportTable(a, b);
    },
    /** 分页 **/
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped></style>
